﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using CP_SchoolForChildren_v0._1.Controllers.Fuzzy;
using CP_SchoolForChildren_v0._1.Controllers.Support;
using CP_SchoolForChildren_v0._1.Models.RelatedModel;
using PagedList;
using Supaman;

namespace CP_SchoolForChildren_v0._1.Controllers.FE
{
    public class SuggestionController : Controller
    {
        //
        // GET: /Suggestion/
        FunctionController fc = new FunctionController();

        // TungThitCho

        [HttpGet]
        public ActionResult SchoolSugesstion(double userEducation, int userQuality, int? page, string sortOrder)
        {
            //if (Request.IsAjaxRequest())
            //{
            //    }
            //else{
            try
            {
            //var fuzzymodule = new FuzzyModule();
                int quality = userQuality;
             var Eduvn = new Education();
            Eduvn.GetInvestLevel(userEducation);
            Eduvn.Display();

            var rule = new Rule();
            rule.DoTheRule(Eduvn.InvestIt, Eduvn.InvestBt,Eduvn.InvestNhieu, quality);
            var fuzzymodule = new FuzzyModule(userEducation);


            fuzzymodule.DoTheWork();
            fuzzymodule.GetInvestLevel(rule.FeeHoiIt, rule.FeeIt, rule.FeeTrungBinh, rule.FeeHoiCao, rule.FeeCao);

            
            
            //fuzzymodule.Suggest(userIncome, userEducation, quality);
            //var ttf = new TuitionFee(fuzzymodule.feeCheap, fuzzymodule.feeAverage, fuzzymodule.feeExpensive, userEducation);
            //ttf.getFee();
            //minFee = ttf.feeMin*userIncome*10000;
            //maxFee = ttf.feeMax*userIncome*10000;
            //getFeeWithDB();
                var record = from sc in fc.db.Schools
                             join st in fc.db.SchoolTypes on sc.TypeID equals st.TypeID
                             join sysr in fc.db.SystemRatings on sc.SchoolID equals sysr.SchoolID
                             join dis in fc.db.Districts on sc.DistrictID equals dis.DistrictID
                             join ct in fc.db.Cities on dis.CityID equals ct.CityID
                             orderby sc.TuitionFee descending 
                             //orderby sysr.SystemRate descending 
                             select new SeachSchoolModel()
                             {
                                 SchoolID = sc.SchoolID,
                                 SchoolName = sc.SchoolName,
                                 TuitionFee = sc.TuitionFee,
                                 TypeName = st.TypeName,
                                 Address = sc.Address,
                                 DistrictName = dis.DistrictName,
                                 CityName = ct.CityName,
                                 SystemRate = sysr.SystemRate
                             };
                int sa = record.Count();
                if(fuzzymodule.isHigh == true)
                {
                    record = record.Where(s => s.TuitionFee >= fuzzymodule.feeMax);
                }else
                {
                    if (fuzzymodule.feeMax < fc.db.Schools.Select(s => s.TuitionFee).Distinct().ToList().Min())
                        fuzzymodule.feeMax = fc.db.Schools.Select(s => s.TuitionFee).Distinct().ToList().Min();
                    
                    record = record.Where(s => s.TuitionFee >= fuzzymodule.feeMin).
                        Where(s => s.TuitionFee <= fuzzymodule.feeMax);
                   // if(fuzzymodule.feeMin > 0){
                    if (quality == 1)
                        record = record.Where(s => s.SystemRate <= fuzzymodule.RateBinhThuong);
                    if (quality == 2)
                        record = record.Where(s => s.SystemRate < fuzzymodule.RateRatCao).Where(s => s.SystemRate > fuzzymodule.RateBinhThuong);
                    if (quality == 3)
                        record = record.Where(s => s.SystemRate > fuzzymodule.RateCao);
                   // }
                }

                    switch (sortOrder)
                    {
                        case "Name desc":
                            record = record.OrderByDescending(s => s.SchoolName);
                            break;
                        case "Rate":
                            record = record.OrderBy(s => s.SystemRate);
                            break;
                        case "Rate desc":
                            record = record.OrderByDescending(s => s.SystemRate);
                            break;
                        case "Fee":
                            record = record.OrderBy(s => s.TuitionFee);
                            break;
                        case "Fee desc":
                            record = record.OrderByDescending(s => s.TuitionFee);
                            break;
                        default:
                            record = record.OrderByDescending(s => s.TuitionFee);
                            break;
                    }
                ViewBag.Count = record.Count();
                int pageSize = 10;
                int pageNumber = (page ?? 1);
                ViewBag.userEducation = userEducation;
                ViewBag.userQuality = userQuality;
                if (Request.IsAjaxRequest())
                {
                    return PartialView("SuggestResult", record.ToPagedList(pageNumber, pageSize));
                }else
                    return View(record.ToPagedList(pageNumber, pageSize));
            }
            catch (Exception ex)
            {
                return View(ex.ToString());
            }
           // }
        }
    }
}
